package com.tyt.travel.controller.v1;

import com.alibaba.fastjson.JSON;
import com.tyt.travel.dto.AddEvaluationDTO;
import com.tyt.travel.dto.scenery.CreateOrUpdateSceneryDTO;
import com.tyt.travel.model.EvaluateEntity;
import com.tyt.travel.model.SceneryEntity;
import com.tyt.travel.service.SceneryService;
import com.tyt.travel.vo.*;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author tianyun
 * @version 1.0
 * @date Created in 2021年03月08日 20:23
 * @since 1.0
 */
@RestController
@RequestMapping("/v1/scenery")
public class SceneryController {

    @Autowired
    SceneryService sceneryService;

    /**
     * 模糊查询景点
     * @param sceneryName
     * @return
     */
    @GetMapping(value = "/searchScenery", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<SearchSceneryVO> searchScenery(@RequestParam(value = "sceneryName", required = false, defaultValue = "") String sceneryName) {
        return sceneryService.searchScenery("%" + sceneryName + "%");
    }

    /**
     * 添加景点
     * @param validator
     * @return
     */
    @PostMapping(value = "/addScenery",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CreatedVO addScenery(@Validated CreateOrUpdateSceneryDTO validator) {
        sceneryService.addScenery(validator);
        return new CreatedVO(12);
    }

    /**
     * 景点详情
     * @return
     */
    @GetMapping("/sceneryDetail")
    public SceneryEntity sceneryDetail(@RequestParam("sceneryId")Integer sceneryId) {
        return sceneryService.sceneryDetail(sceneryId);
    }

    /**
     * 评价景点
     * @return
     */
    @PostMapping(value = "/addEvaluation",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public CreatedVO addEvaluation(@RequestBody AddEvaluationDTO dto) {
        sceneryService.addEvaluation(dto);
        return new CreatedVO(12);
    }

    /**
     * 景点评价列表
     * @return
     */
    @GetMapping("/sceneryEvaluationList")
    public List<EvaluateEntity> sceneryEvaluationList(@RequestParam("sceneryId")Integer sceneryId) {
        return sceneryService.sceneryEvaluationList(sceneryId);
    }

    /**
     * 景点点击计数
     * @return
     */
    @GetMapping("/sceneryCount")
    public UnifyResponseVO sceneryCount(@RequestParam("sceneryId")Integer sceneryId) {
        sceneryService.sceneryCount(sceneryId);
        return new UnifyResponseVO();
    }

    /**
     * 删除景区
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除景区", module = "景区")
    public DeletedVO deleteScenery(@PathVariable("id") @Positive(message = "{id.positive}") Integer id) {
        SceneryEntity scenery = sceneryService.getById(id);
        if (scenery == null) {
            throw new NotFoundException(10022);
        }
        sceneryService.deleteById(id);
        return new DeletedVO(14);
    }

    /**
     * 更新景区
     * @param id
     * @param validator
     * @return
     */
    @PutMapping("/{id}")
    public UpdatedVO updateScenery(@PathVariable("id") @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated CreateOrUpdateSceneryDTO validator) {
        SceneryEntity entity = sceneryService.getById(id);
        if (entity == null) {
            throw new NotFoundException(10022);
        }
        sceneryService.updateScenery(entity, validator);
        return new UpdatedVO(13);
    }

    /**
     * 根据id获取数据
     * @return
     */
    @GetMapping("/{id}")
    public SearchSceneryVO getBook(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        SceneryEntity entity = sceneryService.getById(id);
        if (entity == null) {
            throw new NotFoundException(10022);
        }
        SearchSceneryVO vo = new SearchSceneryVO();
        BeanUtils.copyProperties(entity,vo);
        vo.setSceneryImg(JSON.parseArray(entity.getSceneryImg(), String.class));
        return vo;
    }

}
